<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="是否订阅" prop="subscribe">
        <el-select v-model="queryParams.subscribe" placeholder="请选择用户是否订阅" clearable size="small">
          <el-option
            v-for="dict in subscribeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户标识" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入用户标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户的昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户的昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户的性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入用户所在城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在国家" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入用户所在国家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入用户所在省份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定标识" prop="bind">
        <el-select v-model="queryParams.bind" placeholder="请选择绑定标识" clearable size="small">
          <el-option
            v-for="dict in bindOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="openId" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入openId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['weixin:weixinUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['weixin:weixinUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weixin:weixinUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weixin:weixinUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="weixinUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="是否订阅" align="center" prop="subscribe" :formatter="subscribeFormat" />
      <el-table-column label="用户标识" align="center" prop="openid" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="所在国家" align="center" prop="country" />
      <el-table-column label="所在省份" align="center" prop="province" />
      <el-table-column label="语言" align="center" prop="language" />
      <el-table-column label="头像" align="center" prop="headimgurl" >
        <template slot-scope="scope">
      　　　　<el-image :src="scope.row.headimgurl" width="40" height="40" class="head_pic"/>
    　　</template>
      </el-table-column>
      <el-table-column label="关注时间" align="center" prop="subscribeTime" />
      <el-table-column label="unionid" align="center" prop="unionid" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="分组ID" align="center" prop="groupid" />
      <el-table-column label="分组ID" align="center" prop="tagidList" />
      <el-table-column label="绑定标识" align="center" prop="bind" :formatter="bindFormat" />
      <el-table-column label="登录名  是openId" align="center" prop="username" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['weixin:weixinUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weixin:weixinUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信关注用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息，只有openid和UnionID" prop="subscribe">
          <el-select v-model="form.subscribe" placeholder="请选择用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息，只有openid和UnionID">
            <el-option
              v-for="dict in subscribeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户的标识，对当前公众号唯一" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入用户的标识，对当前公众号唯一" />
        </el-form-item>
        <el-form-item label="用户的昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户的昵称" />
        </el-form-item>
        <el-form-item label="用户的性别，值为1时是男性，值为2时是女性，值为0时是未知" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户的性别，值为1时是男性，值为2时是女性，值为0时是未知">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户所在城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入用户所在城市" />
        </el-form-item>
        <el-form-item label="用户所在国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入用户所在国家" />
        </el-form-item>
        <el-form-item label="用户所在省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入用户所在省份" />
        </el-form-item>
        <el-form-item label="用户的语言，简体中文为zh_CN" prop="language">
          <el-input v-model="form.language" placeholder="请输入用户的语言，简体中文为zh_CN" />
        </el-form-item>
        <el-form-item label="用户头像，最后一个数值代表正方形头像大小" prop="headimgurl">
          <el-input v-model="form.headimgurl" placeholder="请输入用户头像，最后一个数值代表正方形头像大小" />
        </el-form-item>
        <el-form-item label="用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间" prop="subscribeTime">
          <el-input v-model="form.subscribeTime" placeholder="请输入用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间" />
        </el-form-item>
        <el-form-item label="只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。详见：获取用户个人信息" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。详见：获取用户个人信息" />
        </el-form-item>
        <el-form-item label="公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注" />
        </el-form-item>
        <el-form-item label="用户所在的分组ID" prop="groupid">
          <el-input v-model="form.groupid" placeholder="请输入用户所在的分组ID" />
        </el-form-item>
        <el-form-item label="用户所在的分组ID" prop="tagidList">
          <el-input v-model="form.tagidList" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="绑定标识 0 未绑定 1已经绑定" prop="bind">
          <el-select v-model="form.bind" placeholder="请选择绑定标识 0 未绑定 1已经绑定">
            <el-option
              v-for="dict in bindOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登录名  是openId" prop="username">
          <el-input v-model="form.username" placeholder="请输入登录名  是openId" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="名字" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listWeixinUser, getWeixinUser, delWeixinUser, addWeixinUser, updateWeixinUser, exportWeixinUser } from "@/api/weixin/weixinUser";

    export default {
        name: "WeixinUser",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 微信关注用户表格数据
                weixinUserList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息，只有openid和UnionID字典
                subscribeOptions: [],
                // 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知字典
                sexOptions: [],
                // 绑定标识 0 未绑定 1已经绑定字典
                bindOptions: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    subscribe: null,
                    openid: null,
                    nickname: null,
                    sex: null,
                    city: null,
                    country: null,
                    province: null,
                    language: null,
                    headimgurl: null,
                    subscribeTime: null,
                    unionid: null,
                    groupid: null,
                    tagidList: null,
                    bind: null,
                    username: null,
                    password: null,
                    name: null,
                    phone: null,
                    address: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("wx_subscribe_type").then(response => {
                this.subscribeOptions = response.data;
            });
            this.getDicts("wx_sex_type").then(response => {
                this.sexOptions = response.data;
            });
            this.getDicts("wx_bind_type").then(response => {
                this.bindOptions = response.data;
            });
        },
        methods: {
            /** 查询微信关注用户列表 */
            getList() {
                this.loading = true;
                listWeixinUser(this.queryParams).then(response => {
                    this.weixinUserList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息，只有openid和UnionID字典翻译
            subscribeFormat(row, column) {
                return this.selectDictLabel(this.subscribeOptions, row.subscribe);
            },
            // 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知字典翻译
            sexFormat(row, column) {
                return this.selectDictLabel(this.sexOptions, row.sex);
            },
            // 绑定标识 0 未绑定 1已经绑定字典翻译
            bindFormat(row, column) {
                return this.selectDictLabel(this.bindOptions, row.bind);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    subscribe: null,
                    openid: null,
                    nickname: null,
                    sex: null,
                    city: null,
                    country: null,
                    province: null,
                    language: null,
                    headimgurl: null,
                    subscribeTime: null,
                    unionid: null,
                    remark: null,
                    groupid: null,
                    tagidList: null,
                    bind: null,
                    username: null,
                    password: null,
                    name: null,
                    phone: null,
                    address: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加微信关注用户";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getWeixinUser(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改微信关注用户";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateWeixinUser(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addWeixinUser(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除微信关注用户编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return delWeixinUser(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有微信关注用户数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return exportWeixinUser(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        }
    };
</script>
